package net.pulseproject.router.app.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import net.pulseproject.commons.gui.Action;
import net.pulseproject.commons.gui.IconsCache;
import net.pulseproject.commons.gui.SafeCatchingActionListenerFactory;
import net.pulseproject.commons.gui.SafeCatchingActionListenerImpl;
import net.pulseproject.commons.misc.DevelopmentConstants;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class MainMenuBar extends JMenuBar {
	
	private static final long serialVersionUID = 140103346646677165L;
	
	private final SafeCatchingActionListenerFactory actionListenerFactory;
	
	private final JFrame parent;
	
	private final MainMenuBarListener listener;
	
	private final String versionString;
	
	private final boolean osxCompliant;
	
	@Inject
	public MainMenuBar(
			final SafeCatchingActionListenerFactory actionListenerFactory,
			@Assisted final JFrame parent,
			@Assisted final MainMenuBarListener listener,
			@Assisted final String versionString,
			@Assisted final boolean osxCompliant) {
		this.actionListenerFactory = actionListenerFactory;
		if(parent == null) { throw new IllegalArgumentException("parent == null"); }
		this.parent = parent;
		
		if(listener == null) { throw new IllegalArgumentException("listener == null"); }
		this.listener = listener;
		
		if(versionString == null) { throw new IllegalArgumentException("versionString == null"); }
		if(versionString.isEmpty()) { throw new IllegalArgumentException("versionString is empty!"); }
		this.versionString = versionString;
		this.osxCompliant = osxCompliant;
	}


	public final void initCompontents() {
		final ActionListener actionListener =
			this.actionListenerFactory.create(this.parent, "Menubar item action failed!", new Action() {
				@Override public void doAction(final ActionEvent event) {
					final String cmd = event.getActionCommand();
					final MenuItemType type = MenuItemType.byName(cmd);
					type.callback(MainMenuBar.this.listener);
		}});
		
		this.add(this.createApplicationMenu(actionListener));
		this.add(this.createViewMenu(actionListener));
	}
	
	private JMenu createApplicationMenu(final ActionListener actionListener) {
		final JMenu menu = new JMenu("Application");
		
		final JMenuItem itemVersion = new JMenuItem("Version: " + this.versionString);
		itemVersion.setEnabled(false);
		menu.add(itemVersion);
		
		if(this.osxCompliant == false || DevelopmentConstants.ENABLED == true) {
			final JMenuItem itemPreferences = new JMenuItem("Preferences");
			itemPreferences.setActionCommand(MenuItemType.PREFERENCES.name());
			itemPreferences.addActionListener(actionListener);
			menu.add(itemPreferences);
			
			menu.addSeparator();
			
			final JMenuItem itemQuit = new JMenuItem("Quit");
			itemQuit.setActionCommand(MenuItemType.QUIT.name());
			itemQuit.addActionListener(actionListener);
			menu.add(itemQuit);
		}
		
		return menu;
	}

	private JMenu createViewMenu(final ActionListener actionListener) {
		final JMenu menu = new JMenu("View");

		final JMenuItem itemPorts = new JMenuItem("Available Ports");
		menu.add(itemPorts);
		itemPorts.setActionCommand(MenuItemType.AVAILABLE_PORTS.name());
		itemPorts.addActionListener(actionListener);
		
		return menu;
	}
	
}
